class IncidentsController < ApplicationController
  before_filter :check_authentification, :only => [:new, :create]
  before_filter :getmachine, :only => [:new, :create]
  before_filter :getregle, :only => [:new, :create, :agent]
  before_filter :getsalle, :only => [:new, :create, :agent]
  

  # GET /incidents
  # GET /incidents.xml
  def index
    
    if params[:etat] && !params[:etat].empty?
      conditions = " etat = '#{params[:etat]}'"
    end

    if params[:tri] && !params[:tri].empty?
      order = " #{params[:tri]}"
    end
    
    @incidents = Incident.find(:all, :select => "incidents.id,typeincident,proprietecomple,
                                                  datedetection,dateresolution,etat,nommachine",
      :joins => "INNER JOIN machines ON machines.id=incidents.machineid",
      :conditions => conditions,
      :order => order)
    @etat = Incident.find(:all, :select => "etat").map{|e| [e.etat, e.etat]}.uniq
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @incidents }
    end
  end

  # GET /incidents/1
  # GET /incidents/1.xml
  def show
    @incident = Incident.find(:first, :select => "incidents.id,typeincident,proprietecomple,
                                                  datedetection,dateresolution,etat,nommachine",
      :joins => "INNER JOIN machines ON machines.id=incidents.machineid",
      :conditions => "incidents.id = #{params[:id]}")
 
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @incident }
    end
  end

  # GET /incidents/new
  # GET /incidents/new.xml
  def new
    @incident = Incident.new
  end

  # GET /incidents/1/edit
  def edit
    @incident = Incident.find(params[:id])
  end

  # POST /incidents
  # POST /incidents.xml
  def create

    #Si nous venons de choisir une salle
    if params[:machineid] == "0" && params[:salleid] != "0"
      @incident = Incident.new
      @machines = Machine.find(:all, :select => "id, nommachine", :conditions => "salleid=#{params[:salleid].to_i}").map { |m| [m.nommachine, m.id] }
      respond_to do |format|
        format.html {render :action => :new}
        format.js {render :action => "actualiser.rjs"}
      end

      #Verifie que le salle et/ou la machine est sélectionné
    elsif params[:machineid] == "0" && params[:salleid] == "0"
      @incident = Incident.new
      flash[:error] = "Veuillez sélectionner une salle et/ou une machine."
      respond_to do |format|
        format.html { render :action => "new" }
        format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
      end
    elsif params[:regleid].eql?("0")
      @incident = Incident.new
      flash[:error] = "Veuillez sélectionner une règle."
      respond_to do |format|
        format.html { render :action => "new", :machineid => params[:machineid] }
        format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
      end
    else
      params[:incident][:etat] = 'a traiter'
      params[:incident][:machineid] = params[:machineid]

      save_incident = []
      for regle in params[:regleid]
        params[:incident][:typeincident] = Regle.find(:first, :select => "descregle", :conditions => "id=#{regle}").descregle
        params[:incident][:regleid] = regle

        @incident = Incident.new(params[:incident])

        save_incident += [@incident.save]
      end

      respond_to do |format|
        if !save_incident.include?(false)
          flash[:notice] = "L'incident à été créé avec succès"
          format.html { redirect_to(@incident) }
          format.xml  { render :xml => @incident, :status => :created, :location => @incident }
        else
          flash[:error] = "L'incident n'a pas été créé"
          format.html { render :action => "new" }
          format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
        end
      end
    end
  end

  # PUT /incidents/1
  # PUT /incidents/1.xml
  def update
    @incident = Incident.find(params[:id])

    respond_to do |format|
      if @incident.update_attributes(params[:incident])
        flash[:notice] = 'Mise à jour réussi.'
        format.html { redirect_to(@incident) }
        format.xml  { render :xml => @incident }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @incident.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /incidents/1
  # DELETE /incidents/1.xml
  def destroy
    @incident = Incident.find(params[:id])
    @incident.destroy

    respond_to do |format|
      format.html { redirect_to(incidents_url) }
      format.xml  { render :xml => @incident }
    end
  end

  def getmachine
    @machines = Machine.find(:all, :select => "id, nommachine").map { |m| [m.nommachine, m.id] }
  end

  def getsalle
    @salles = Salle.find(:all, :select => "id, nomsalle").map { |s| [s.nomsalle, s.id] }
  end

  def getregle
    @regles = Regle.find(:all).map { |r| ["#{r.descregle} #{r.operationarithme} #{r.valeurlimite}", r.id] }
  end

  def interroger
    incidents, @params_inci = Incident.agent("machine", params[:espace_restant],params[:ram_utilise],params[:cpu_utilise], params[:agent_operationnel], params[:etat_reseau])
    redirect_to :controller => :machines, :action => "interomachine", :id => params[:machineid], :tab_incidents => incidents, :param_inci => @params_inci
  end

  def recherche
    redirect_to :action => "index", :etat => params[:etat], :tri => params[:tri]
  end
end
